package com.huo.thread.cyclicbarrierdemo;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Demo {
    public void meeting(CyclicBarrier cyclicBarrier){
        System.out.println(Thread.currentThread().getName() + "等待，准备开会");
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() +"发言");
    }

    public static void main(String[] args) {
        Demo d = new Demo();
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5, new Runnable() {
            @Override
            public void run() {
                System.out.println("开始开会。。。");
            }
        });

        for(int i = 0 ; i < 10  ; i ++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    d.meeting(cyclicBarrier);
                }
            }).start();
        }
    }
}
